package com.purong.purong.fragment;

import android.content.Intent;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.purong.purong.Contant;
import com.purong.purong.R;
import com.purong.purong.activity.ForgetPwdActivity;
import com.purong.purong.activity.RegisterActivity;
import com.purong.purong.bean.LoginBean;
import com.purong.purong.util.CommonUtil;
import com.umeng.socialize.utils.Log;
import com.yao.sdk.net.ResponseInfo;

import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;

import cn.jpush.android.api.JPushInterface;
import cn.jpush.android.api.TagAliasCallback;

/**
 * Created by zengwenjie on 2017/11/30.
 */
public class LoginPwdFragment extends BaseFragment {
    private EditText phoneEd,pwdEd;
    private ImageView phoneClear,pwdClear;
    private View pwdLine,phoneLine;
    private Button loginBtn;
    private TextView regist,forgetPwd;
    private boolean phoneInput,pwdInput;
    @Override
    public int getRootViewId() {
        return R.layout.login_pwd_fragment;
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode==1){
            if (resultCode==getActivity().RESULT_OK){
                String phone=data.getStringExtra("PHONE");
                String pwd=data.getStringExtra("PWD");
                if (phone!=null&&pwd!=null){
                    phoneEd.setText(phone);
                    pwdEd.setText(pwd);
                    login(phone,pwd);
                }
            }
        }
    }

    @Override
    public void initView(View rootView) {
        phoneLine=(View)rootView.findViewById(R.id.phone_cut_line);
        pwdLine=(View)rootView.findViewById(R.id.pwd_cut_line);
        regist=(TextView)rootView.findViewById(R.id.register);
        regist.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(getActivity(), RegisterActivity.class);
                startActivityForResult(intent,1);
            }
        });
        forgetPwd=(TextView)rootView.findViewById(R.id.forget_pwd);
        forgetPwd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(getActivity(), ForgetPwdActivity.class);
                startActivity(intent);
            }
        });
        phoneClear=(ImageView)rootView.findViewById(R.id.phone_clear);
        phoneClear.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                phoneEd.setText("");
            }
        });
        pwdClear=(ImageView)rootView.findViewById(R.id.pwd_clear);
        pwdClear.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                pwdEd.setText("");
            }
        });
        phoneEd=(EditText)rootView.findViewById(R.id.phone_ed);
        pwdEd=(EditText)rootView.findViewById(R.id.pwd_ed);
        loginBtn=(Button)rootView.findViewById(R.id.login);
        loginBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!phoneInput) {
                    showToast("请输入手机号");
                    return;
                }
                if (!CommonUtil.isPhoneNum(phoneEd.getText().toString())) {
                    showToast("请输入正确的手机号");
                    return;
                }
                if (!pwdInput) {
                    showToast("请输入密码");
                    return;
                }
                login(phoneEd.getText().toString(),pwdEd.getText().toString());
            }
        });
        phoneEd.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (s.toString().trim().equals("")) {
                    phoneInput = false;
                    phoneClear.setVisibility(View.GONE);
                } else {
                    phoneInput = true;
                    phoneClear.setVisibility(View.VISIBLE);
                }
                if (s.length() == 11) {
                    if (pwdEd.getText().toString().equals("")) {
                        pwdEd.requestFocus();
                    }
                }
                if (phoneInput && pwdInput) {
                    loginBtn.setBackgroundResource(R.drawable.button_blue_radius5);
                } else {
                    loginBtn.setBackgroundResource(R.drawable.button_gray_radius5);

                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
        pwdEd.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (s.toString().trim().equals("")) {
                    pwdInput = false;
                    pwdClear.setVisibility(View.GONE);
                } else {
                    pwdInput = true;
                    pwdClear.setVisibility(View.VISIBLE);
                }
                if (phoneInput && pwdInput) {
                    loginBtn.setBackgroundResource(R.drawable.button_blue_radius5);
                } else {
                    loginBtn.setBackgroundResource(R.drawable.button_gray_radius5);

                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
        phoneEd.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (hasFocus) {
                    phoneLine.setBackgroundColor(getActivity().getResources().getColor(R.color.blue));
                } else {
                    phoneLine.setBackgroundColor(getActivity().getResources().getColor(R.color.cut_line));
                }
            }
        });
        pwdEd.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (hasFocus) {
                    pwdLine.setBackgroundColor(getActivity().getResources().getColor(R.color.blue));
                } else {
                    pwdLine.setBackgroundColor(getActivity().getResources().getColor(R.color.cut_line));

                }
            }
        });
    }

    @Override
    public void initData() {

    }
    private void login(String phone,String pwd){
        Map<String, String> params = new LinkedHashMap<String, String>();
        params.put("user_phone",phone);
        params.put("user_pwd",pwd);
        request(Contant.LOGIN, params, true,true);
    }
    @Override
    public void onResponse(ResponseInfo responseInfo) {
        if (hasDestroy())
            return;
        super.onResponse(responseInfo);
        if (responseInfo.url.equals(Contant.LOGIN)){
            if (responseInfo.isRequestOk){
                try{
                    LoginBean loginBean= JSON.parseObject(responseInfo.content,LoginBean.class);
                    loginBean.setPwd(responseInfo.params.get("user_pwd"));
                    loginBean.save(getActivity());
                    JPushInterface.setAlias(getActivity(),0,loginBean.getUser_id());
                    showToast("登录成功");
                    getActivity().finish();
                }catch (Exception e){

                }
            }
        }
    }

}
